package Demo03.Demo04;

public class DrawThread extends Thread {
    //模拟用户账户
    private Account account;
    //当前取钱线程所希望的取钱数
    public double drawAmout;

    public DrawThread(String name, Account account, double drawAmout) {
        super(name);
        this.account = account;
        this.drawAmout = drawAmout;
    }
    //当多个线程修改一个共享数据时，将涉及线程安全问题
    @Override
    public void run() {
        //余额大于取钱数目
        if (account.getBalnce()>=drawAmout){
            //吐出钞票
            System.out.println("放出钞票"+drawAmout);

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            //修改余额
            account.setBalnce(account.getBalnce()-drawAmout);

            System.out.println("\t 余额为："+account.getBalnce());
        }
        else {
            System.out.println(getName()+"取钱失败，余额不足");
        }
    }
}
